//
//  TabBar.m
//  LotteryClient
//
//  Created by Suny on 14-7-5.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "TabBar.h"
#import "UIColor+BaseFramework.h"
#import "UIImage+BaseFramework.h"
#import "UIView+BaseFramework.h"

#define kTabBarItemYOffset 0

@interface TabBar ()
{
    UIImageView *_backgroundView;
    UIImageView *_shadowView;
    UIImageView *_selectionIndicatorView;

    NSMutableArray *_buttonItems;

}

@property(nonatomic, strong) NSMutableArray *buttonItems;

- (void)initTabBarItems;

- (void)didClickTabBarItem:(TabBarItem *)item;

- (void)setSelectedItem:(TabBarItem *)selectedItem animated:(BOOL)animated;

@end

@implementation TabBar

@synthesize delegate = _delegate;
@synthesize items = _items;
@synthesize buttonItems = _buttonItems;
@synthesize selectedItem = _selectedItem;
@synthesize backgroundImage = _backgroundImage;
@synthesize selectionIndicatorImage = _selectionIndicatorImage;
@synthesize shadowImage = _shadowImage;
@synthesize animateSelection = _animateSelection;
@dynamic selectedIndex;

#pragma mark life cycle

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        _buttonItems = [[NSMutableArray alloc] initWithCapacity:5];
//        if ([UIDevice currentDevice].sn_shouldEnableTabBarBlurEffect)
//        {
//            SNBlurView * blurView = [[SNBlurView alloc] initWithFrame:self.bounds];
//            [blurView setBlurTintColor:[UIColor colorWithHex:0xf9f8f9]];
//            //            self.backgroundColor = [UIColor colorWithHex:0xf9f8f9];
//            [self addSubview:blurView];
//            [blurView release];
//        }
//        else
        {
            self.backgroundColor = [UIColor colorWithHex:0xf9f8f9];
        }

        UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 0.5)];
        separatorLine.backgroundColor = [UIColor colorWithHex:0xb2b2b2];
        [self addSubview:separatorLine];
    }
    return self;
}

- (void)dealloc
{
    _delegate = nil;

}

#pragma mark Synthesize

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = backgroundImage;

    if (backgroundImage)
    {
        if (!_backgroundView)
        {
            _backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
            [self insertSubview:_backgroundView
                        atIndex:0];
        }

        _backgroundView.image = [backgroundImage stretchableImageByCenter];
    }
    else
    {
        [_backgroundView removeFromSuperview];
        _backgroundView, _backgroundView = nil;
    }
}

- (void)setShadowImage:(UIImage *)shadowImage
{
    _shadowImage = shadowImage;

    if (shadowImage)
    {
        if (!_shadowView)
        {
            _shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                        -shadowImage.size.height,
                                                                        CGRectGetWidth(self.bounds),
                                                                        shadowImage.size.height)];
            [self addSubview:_shadowView];
        }

        _shadowView.image = [shadowImage stretchableImageByWidthCenter];
    }
    else
    {
        [_shadowView removeFromSuperview];
        _shadowView, _shadowView = nil;
    }
}

- (void)setSelectionIndicatorImage:(UIImage *)selectionIndicatorImage
{
    _selectionIndicatorImage = selectionIndicatorImage;

    if (selectionIndicatorImage)
    {
        if (!_selectionIndicatorView)
        {
            CGRect frame            = CGRectZero;
            if (self.selectedItem)
            {
                frame = self.selectedItem.frame;
            }
            _selectionIndicatorView = [[UIImageView alloc] initWithFrame:frame];
            if (_backgroundView)
            {
                [self insertSubview:_selectionIndicatorView
                       aboveSubview:_backgroundView];
            }
            else
            {
                [self insertSubview:_selectionIndicatorView
                            atIndex:0];
            }
        }

        _selectionIndicatorView.image = [selectionIndicatorImage stretchableImageByCenter];
    }
    else
    {
        [_selectionIndicatorView removeFromSuperview];
        _selectionIndicatorView, _selectionIndicatorView = nil;
    }
}

- (NSUInteger)selectedIndex
{
    return [self.items indexOfObject:self.selectedItem];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if (selectedIndex < self.items.count && self.selectedIndex != selectedIndex)
    {
        [self setSelectedItem:self.items[selectedIndex]
                     animated:NO];
    }
}

- (void)setItems:(NSArray *)items animated:(BOOL)animated
{
    if (animated)
    {
        [UIView animateWithDuration:0.25
                              delay:0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
            self.items = items;
        }
                         completion:^(BOOL finished) {

        }];
    }
    else
    {
        self.items = items;
    }
}

- (void)setItems:(NSArray *)items
{
    if (![_items isEqualToArray:items])
    {
        _items = items;

        [self initTabBarItems];
    }
}

- (void)setSelectedItem:(TabBarItem *)selectedItem animated:(BOOL)animated
{
    if ([self.selectedItem isEqual:selectedItem])
    {
        [self.selectedItem setSelected:YES];
    }
    else
    {
        [self.selectedItem setSelected:NO];
        [selectedItem setSelected:YES];

        self.selectedItem = selectedItem;
    }
}

- (void)initTabBarItems
{
    [self setSelectedItem:nil
                 animated:NO];
    [self.buttonItems makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.buttonItems removeAllObjects];

    NSUInteger count = [self.items count];

    CGFloat  itemWidht  = self.bounds.size.width / count;
    CGFloat  itemHeight = self.bounds.size.height - kTabBarItemYOffset;

    int centerIndex = -1;
    
    if (self.showBigItemCenter && count%2 !=0)
    {
        centerIndex = (int) floor(count/2.0);
    }
    for (int i          = 0; i < self.items.count; i++)
    {
        TabBarItem *item = [self.items objectAtIndex:i];
        if (i == centerIndex)
        {
            item.frame = CGRectMake(itemWidht * i, - 2 * (kTabBarItemYOffset + 5), itemHeight + 2*(kTabBarItemYOffset + 5),
									itemHeight + 2*(kTabBarItemYOffset + 5));
			item.centerX = self.width/2;
            item.bigItemCenter = YES;
        }
        else
        {
            item.frame = CGRectMake(itemWidht * i, kTabBarItemYOffset, itemHeight, itemHeight);
			item.centerX = itemWidht * i + itemWidht /2;
            item.bigItemCenter = NO;
        }
        [item addTarget:self
                 action:@selector(didClickTabBarItem:)
       forControlEvents:UIControlEventTouchDown];
        [self addSubview:item];
        [self.buttonItems addObject:item];

        if (i == 0)
        {
            [self setSelectedItem:item
                         animated:NO];
        }
    }
}


- (void)didClickTabBarItem:(TabBarItem *)item
{
	if ([self.delegate respondsToSelector:@selector(tabBar:willSelectItemAtIndex:tabBarItem:)])
	{
		if ([self.delegate tabBar:self
			willSelectItemAtIndex:[self.items indexOfObject:item]
					   tabBarItem:item])
		{
			[self setSelectedItem:item
						 animated:YES];
			if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemAtIndex:tabBarItem:)])
			{
				[self.delegate tabBar:self
				 didSelectItemAtIndex:[self.items indexOfObject:item]
						   tabBarItem:item];
			}
		}
	}
	else
	{
		[self setSelectedItem:item
					 animated:YES];
		if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemAtIndex:tabBarItem:)])
		{
			[self.delegate tabBar:self
			 didSelectItemAtIndex:[self.items indexOfObject:item]
					   tabBarItem:item];
		}
	}
	

}

@end

